import { EventEmitter, IEventListener } from '../engine/common/event-emmiter';
import { Engine } from '../engine/engine';
import { DemoWorld } from './worlds/demo.world';

export class Game extends Engine {
  #demoWorld: DemoWorld;
  #listeners: IEventListener[] = [];

  readonly onGameOver = new EventEmitter<boolean>();

  constructor(private readonly canvasElement: HTMLCanvasElement) {
    super();

    this.#demoWorld = new DemoWorld(canvasElement);
    this.#listeners.push(this.#demoWorld.onComplete.listen((isVictorious) => this.onGameOver.emit(isVictorious)));
    this.attachWorld(this.#demoWorld);
  }

  finish() {
    super.finish();

    this.#listeners.forEach((listener) => listener.dispose());
  }
}
